﻿using System;
using System.Collections.Generic;
using Hematology.Data;
using Hematology.Data.Interface;
using Hematology.Tools;
using Hematology.Tools.Storage;
using NgoResource = Hematology.UserSite.App_GlobalResources.UserSite;

namespace Hematology.UserSite.controls
{
	public partial class ProfileItem : System.Web.UI.UserControl
	{
		public EnumHelper.ProfileItemKey TargetProfileItem { get; set; }

		private void FillValues(string sName, string sValue, bool bIsTextBox)
		{
			lblValueName.Text = sName;
			lblValueRead.Text = !String.IsNullOrEmpty(sValue) ? sValue : NgoResource.resWhitespace;
			txtValueRead.Text = !String.IsNullOrEmpty(sValue) ? sValue : NgoResource.resWhitespace;

			if (TargetProfileItem == EnumHelper.ProfileItemKey.FirstName ||
				TargetProfileItem == EnumHelper.ProfileItemKey.MiddleName ||
				TargetProfileItem == EnumHelper.ProfileItemKey.LastName ||
				TargetProfileItem == EnumHelper.ProfileItemKey.BirthDate ||
				TargetProfileItem == EnumHelper.ProfileItemKey.Email ||
				TargetProfileItem == EnumHelper.ProfileItemKey.ContactPhone ||
				TargetProfileItem == EnumHelper.ProfileItemKey.AdditionalPhone ||
				TargetProfileItem == EnumHelper.ProfileItemKey.Skype)
			{
				pnlTextCell.CssClass = "textCellName";
				pnlValueCell.CssClass = "valueCellName";
			}
		}

		//TODO: Method for filling GeoItem
		private void FillGeoItem(IGeoItem geoItem)
		{
			if (geoItem is Country)
			{
				lblValueName.Text = NgoResource.resJobCountry;
			}
			else if (geoItem is Region)
			{
				lblValueName.Text = NgoResource.resJobRegion;
			}
			else if (geoItem is City)
			{
				lblValueName.Text = NgoResource.resJobCity;
			}

			lblValueRead.Text = NgoResource.resWhitespace;

			if (SessionHelper.ActiveUser.JobId != null)
			{
				Job userJob = Job.Get(SessionHelper.ActiveUser.JobId.Value);
				if (userJob != null && userJob.CountryId != null)
				{
					Country userJobCountry = Country.Get(userJob.CountryId.Value);

					if (userJobCountry != null)
					{
						lblValueRead.Text = userJobCountry.Name;
					}
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				switch (TargetProfileItem)
				{
					case EnumHelper.ProfileItemKey.Email:
						{
							FillValues(NgoResource.resEmail, SessionHelper.ActiveUser.BaseInfo.Email, true);
							mvProfileItemAction.ActiveViewIndex = 2;
							break;
						}
					case EnumHelper.ProfileItemKey.FirstName:
						{
							FillValues(NgoResource.resFirstName, SessionHelper.ActiveUser.FirstName, true);
							break;
						}
					case EnumHelper.ProfileItemKey.MiddleName:
						{
							FillValues(NgoResource.resMiddleName, SessionHelper.ActiveUser.MiddleName, true);
							break;
						}
					case EnumHelper.ProfileItemKey.LastName:
						{
							FillValues(NgoResource.resLastName, SessionHelper.ActiveUser.LastName, true);
							break;
						}
					case EnumHelper.ProfileItemKey.JobCountry:
						{
							lblValueName.Text = NgoResource.resJobCountry;
							lblValueRead.Text = NgoResource.resWhitespace;

							if (SessionHelper.ActiveUser.JobId != null)
							{
								Job userJob = Job.Get(SessionHelper.ActiveUser.JobId.Value);
								if (userJob != null && userJob.CountryId != null)
								{
									Country userJobCountry = Country.Get(userJob.CountryId.Value);

									if (userJobCountry != null)
									{
										lblValueRead.Text = userJobCountry.Name;
									}
								}
							}

							break;
						}
					case EnumHelper.ProfileItemKey.JobRegion:
						{
							lblValueName.Text = NgoResource.resJobRegion;
							lblValueRead.Text = NgoResource.resWhitespace;

							if (SessionHelper.ActiveUser.JobId != null)
							{
								Job userJob = Job.Get(SessionHelper.ActiveUser.JobId.Value);
								if (userJob != null && userJob.RegionId != null)
								{
									Region userJobRegion = Region.Get(userJob.RegionId.Value);

									if (userJobRegion != null)
									{
										lblValueRead.Text = userJobRegion.Name;
									}
								}
							}

							break;
						}
					case EnumHelper.ProfileItemKey.JobCity:
						{
							lblValueName.Text = NgoResource.resJobCity;
							lblValueRead.Text = NgoResource.resWhitespace;

							if (SessionHelper.ActiveUser.JobId != null)
							{
								Job userJob = Job.Get(SessionHelper.ActiveUser.JobId.Value);
								if (userJob != null && userJob.CityId != null)
								{
									City userJobCity = City.Get(userJob.CityId.Value);

									if (userJobCity != null)
									{
										lblValueRead.Text = userJobCity.Name;
									}
								}
							}

							break;
						}
					case EnumHelper.ProfileItemKey.Job:
						{
							lblValueName.Text = NgoResource.resJob;
							lblValueRead.Text = NgoResource.resWhitespace;

							if (SessionHelper.ActiveUser.JobId != null)
							{
								Job userJob = Job.Get(SessionHelper.ActiveUser.JobId.Value);
								if (userJob != null && userJob.CityId != null)
								{
									lblValueRead.Text = userJob.FullName;
								}
							}

							break;
						}
					case EnumHelper.ProfileItemKey.JobPosition:
						{
							FillValues(NgoResource.resJobPosition, SessionHelper.ActiveUser.JobPosition, true);
							break;
						}
					case EnumHelper.ProfileItemKey.BirthDate:
						{
							FillValues(NgoResource.resBirthDate,
									   SessionHelper.ActiveUser.BirthDate.HasValue
										? SessionHelper.ActiveUser.BirthDate.Value.ToLongDateString()
										: NgoResource.resWhitespace, false);
							break;
						}
					case EnumHelper.ProfileItemKey.ContactPhone:
						{
							FillValues(NgoResource.resPhone1, SessionHelper.ActiveUser.Phone1, true);
							break;
						}
					case EnumHelper.ProfileItemKey.AdditionalPhone:
						{
							FillValues(NgoResource.resPhone2, SessionHelper.ActiveUser.Phone2, true);
							break;
						}
					case EnumHelper.ProfileItemKey.Skype:
						{
							FillValues(NgoResource.resSkype, SessionHelper.ActiveUser.Skype, true);
							break;
						}
				}
				mvProfileItem.ActiveViewIndex = 0;
				mvProfileItemAction.ActiveViewIndex = 0;
			}
		}

		protected void btnProfileItemChange_Click(object sender, EventArgs e)
		{
			switch (TargetProfileItem)
			{
				case EnumHelper.ProfileItemKey.JobCountry:
					if (SessionHelper.ActiveUser.JobId != null)
					{
						Job userJob = Job.Get(SessionHelper.ActiveUser.JobId.Value);
						if (userJob != null && userJob.CountryId != null)
						{
							List<Country> countryList = Country.GetAllCountries(true);

							ddlValueRead.DataTextField = "Name";
							ddlValueRead.DataValueField = "Id";

							ddlValueRead.DataSource = countryList;
							ddlValueRead.DataBind();

							ddlValueRead.SelectedIndex = countryList.FindIndex(country => country.Id == userJob.CountryId.Value);
						}
					}

					mvProfileItem.ActiveViewIndex = 2;
					break;
				case EnumHelper.ProfileItemKey.JobRegion:
					if (SessionHelper.ActiveUser.JobId != null)
					{
						Job userJob = Job.Get(SessionHelper.ActiveUser.JobId.Value);
						if (userJob != null && userJob.CountryId != null)
						{
							List<Region> regionList = Region.GetAllRegions(userJob.CountryId.Value, true);

							ddlValueRead.DataTextField = "Name";
							ddlValueRead.DataValueField = "Id";

							ddlValueRead.DataSource = regionList;
							ddlValueRead.DataBind();

							ddlValueRead.SelectedIndex = regionList.FindIndex(region => userJob.RegionId != null && region.Id == userJob.RegionId.Value);
						}
					}

					mvProfileItem.ActiveViewIndex = 2;
					break;
				case EnumHelper.ProfileItemKey.JobCity:
					if (SessionHelper.ActiveUser.JobId != null)
					{
						Job userJob = Job.Get(SessionHelper.ActiveUser.JobId.Value);
						if (userJob != null && userJob.CountryId != null)
						{
							List<City> cityList = City.GetAllCities(userJob.CountryId.Value, null, true);

							ddlValueRead.DataTextField = "Name";
							ddlValueRead.DataValueField = "Id";

							ddlValueRead.DataSource = cityList;
							ddlValueRead.DataBind();

							ddlValueRead.SelectedIndex = cityList.FindIndex(city => userJob.CityId != null && city.Id == userJob.CityId.Value);

						}
					}

					mvProfileItem.ActiveViewIndex = 2;
					break;
				case EnumHelper.ProfileItemKey.Job:
					if (SessionHelper.ActiveUser.JobId != null)
					{
						Job userJob = Job.Get(SessionHelper.ActiveUser.JobId.Value);

						List<Job> lJobList = new List<Job>();

						if (userJob != null && userJob.CityId.HasValue && userJob.CountryId.HasValue)
						{
							lJobList = Job.GetAllJobs(userJob.CountryId.Value, null, userJob.CityId.Value);
						}
						else if (userJob != null && userJob.CountryId.HasValue)
						{
							lJobList = Job.GetAllJobs(userJob.CountryId.Value, null, null);
						}

						if (lJobList != null)
						{
							ddlValueRead.DataTextField = "Name";
							ddlValueRead.DataValueField = "JobId";

							ddlValueRead.DataSource = lJobList;
							ddlValueRead.DataBind();

							ddlValueRead.SelectedIndex = lJobList.FindIndex(i =>
							                                                userJob != null && userJob.JobId != null &&
							                                                i.JobId == userJob.JobId.Value);
						}
					}

					mvProfileItem.ActiveViewIndex = 2;
					break;
				case EnumHelper.ProfileItemKey.BirthDate:
					if (SessionHelper.ActiveUser.BirthDate != null)
					{
						lblValueRead.Text =
							SessionHelper.ActiveUser.BirthDate.Value.ToLongDateString();
					}

					break;
				default:
					mvProfileItem.ActiveViewIndex = 1;
					break;
			}

			mvProfileItemAction.ActiveViewIndex = 1;
		}

		protected void btnProfileItemSave_Click(object sender, EventArgs e)
		{
			switch (TargetProfileItem)
			{
				case EnumHelper.ProfileItemKey.Email:
					{
						SessionHelper.ActiveUser.BaseInfo.Email = txtValueRead.Text.Trim();
						lblValueRead.Text = txtValueRead.Text.Trim();
						break;
					}
				case EnumHelper.ProfileItemKey.FirstName:
					{
						SessionHelper.ActiveUser.FirstName = txtValueRead.Text.Trim();
						lblValueRead.Text = txtValueRead.Text.Trim();
						break;
					}
				case EnumHelper.ProfileItemKey.MiddleName:
					{
						SessionHelper.ActiveUser.MiddleName = txtValueRead.Text.Trim();
						lblValueRead.Text = txtValueRead.Text.Trim();
						break;
					}
				case EnumHelper.ProfileItemKey.LastName:
					{
						SessionHelper.ActiveUser.LastName = txtValueRead.Text.Trim();
						lblValueRead.Text = txtValueRead.Text.Trim();
						break;
					}
				case EnumHelper.ProfileItemKey.JobCountry:
					{
						lblValueRead.Text = ddlValueRead.SelectedItem.Text;
						break;
					}
				case EnumHelper.ProfileItemKey.JobRegion:
					{
						lblValueRead.Text = ddlValueRead.SelectedItem.Text;
						break;
					}
				case EnumHelper.ProfileItemKey.JobCity:
					{
						lblValueRead.Text = ddlValueRead.SelectedItem.Text;
						break;
					}
				case EnumHelper.ProfileItemKey.Job:
					{
						SessionHelper.ActiveUser.JobId = Convert.ToInt32(ddlValueRead.SelectedItem.Value);
						lblValueRead.Text = ddlValueRead.SelectedItem.Text;
						break;
					}
				case EnumHelper.ProfileItemKey.JobPosition:
					{
						SessionHelper.ActiveUser.JobPosition = txtValueRead.Text.Trim();
						lblValueRead.Text = txtValueRead.Text.Trim();
						break;
					}
				case EnumHelper.ProfileItemKey.ContactPhone:
					{
						SessionHelper.ActiveUser.Phone1 = txtValueRead.Text.Trim();
						lblValueRead.Text = txtValueRead.Text.Trim();
						break;
					}
				case EnumHelper.ProfileItemKey.AdditionalPhone:
					{
						SessionHelper.ActiveUser.Phone2 = txtValueRead.Text.Trim();
						lblValueRead.Text = txtValueRead.Text.Trim();
						break;
					}
				case EnumHelper.ProfileItemKey.Skype:
					{
						SessionHelper.ActiveUser.Skype = txtValueRead.Text.Trim();
						lblValueRead.Text = txtValueRead.Text.Trim();
						break;
					}
			}
			mvProfileItem.ActiveViewIndex = 0;
			mvProfileItemAction.ActiveViewIndex = 0;
		}

		protected void btnProfileItemCancel_Click(object sender, EventArgs e)
		{
			mvProfileItem.ActiveViewIndex = 0;
			mvProfileItemAction.ActiveViewIndex = 0;
		}
	}
}